/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012 Oliver Katz */

#include "layout_engine.h"

namespace sleepyface
{
	namespace visual
	{
		namespace layout_engine
		{
			Container::Container()
			{
			}

			Container::Container(string n)
			{
				name = n;
			}

			string Container::getName()
			{
				return name;
			}

			string Container::setName(string n)
			{
				return (name = n);
			}

			int Container::appendContainer(Container c)
			{
				pair<Widget, Container> subtmp;
				subtmp.second = c;
				pair<bool, pair<Widget, Container> > tmp;
				tmp.first = false;
				tmp.second = subtmp;
				branches.push_back(tmp);
				return branches.size()-1;
			}

			int Container::appendWidget(Widget w)
			{
				pair<Widget, Container> subtmp;
				subtmp.first = w;
				pair<bool, pair<Widget, Container> > tmp;
				tmp.first = true;
				tmp.second = subtmp;
				branches.push_back(tmp);
				return branches.size()-1;
			}

			int Container::size()
			{
				return branches.size();
			}

			bool Container::isContainer(int idx)
			{
				return (!branches[idx].first);
			}

			bool Container::isWidget(int idx)
			{
				return branches[idx].first;
			}

			Container Container::getContainer(int idx)
			{
				return branches[idx].second.second;
			}

			Widget Container::getWidget(int idx)
			{
				return branches[idx].second.first;
			}

			bool Container::isValue(string k)
			{
				return (isInt(k) || isString(k));
			}

			bool Container::isInt(string k)
			{
				return (ivalues.find(k) != ivalues.end());
			}

			bool Container::isString(string k)
			{
				return (svalues.find(k) != svalues.end());
			}

			int Container::getInt(string k)
			{
				if (isInt(k))
					return ivalues[k];
				else
					return 0;
			}

			int Container::setInt(string k, int v)
			{
				if (isInt(k))
					return (ivalues[k] = v);
				else
					return 0;
			}

			string Container::getString(string k)
			{
				if (isString(k))
					return svalues[k];
				else
					return "";
			}

			string Container::setString(string k, string v)
			{
				if (isString(k))
					return (svalues[k] = v);
				else
					return "";
			}

			string Container::display(int n)
			{
				stringstream ss;
				for (int i = 0; i < n; i++)
					ss << " ";
				ss << name << "\n";
				for (map<string, int>::iterator i = ivalues.begin(); i != ivalues.end(); i++)
				{
					for (int j = 0; j < n+1; j++)
						ss << " ";
					ss << i->first << " <= " << i->second << "\n";
				}
				for (map<string, string>::iterator i = svalues.begin(); i != svalues.end(); i++)
				{
					for (int j = 0; j < n+1; j++)
						ss << " ";
					ss << i->first << " <= \"" << i->second << "\"\n";
				}
				for (int i = 0; i < size(); i++)
				{
					if (isContainer(i))
					{
						getContainer(i).display(n+1);
					}
					else
					{
						getWidget(i).display(n+1);
					}
				}
				return ss.str();
			}

			string Container::display()
			{
				return display(0);
			}

			Widget::Widget()
			{
			}

			Widget::Widget(string n, string t)
			{
				name = n;
				type = t;
			}

			string Widget::getName()
			{
				return name;
			}

			string Widget::setName(string n)
			{
				return (name = n);
			}

			string Widget::getType()
			{
				return type;
			}

			string Widget::setType(string t)
			{
				return (type = t);
			}

			bool Widget::isValue(string k)
			{
				return (isInt(k) || isString(k));
			}

			bool Widget::isInt(string k)
			{
				return (ivalues.find(k) != ivalues.end());
			}

			bool Widget::isString(string k)
			{
				return (svalues.find(k) != svalues.end());
			}

			int Widget::getInt(string k)
			{
				if (isInt(k))
					return ivalues[k];
				else
					return 0;
			}

			int Widget::setInt(string k, int v)
			{
				if (isInt(k))
					return (ivalues[k] = v);
				else
					return 0;
			}

			string Widget::getString(string k)
			{
				if (isString(k))
					return svalues[k];
				else
					return "";
			}

			string Widget::setString(string k, string v)
			{
				if (isString(k))
					return (svalues[k] = v);
				else
					return "";
			}

			string Widget::display(int n)
			{
				stringstream ss;
				for (int i = 0; i < n; i++)
					ss << " ";
				ss << name << "(" << type << ")\n";
				for (map<string, int>::iterator i = ivalues.begin(); i != ivalues.end(); i++)
				{
					for (int j = 0; j < n+1; j++)
						ss << " ";
					ss << i->first << " <= " << i->second << "\n";
				}
				for (map<string, string>::iterator i = svalues.begin(); i != svalues.end(); i++)
				{
					for (int j = 0; j < n+1; j++)
						ss << " ";
					ss << i->first << " <= \"" << i->second << "\"\n";
				}
				return ss.str();
			}

			string Widget::display()
			{
				return display(0);
			}
		}
	}
}
